<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout"
      layout:decorate="~{layout/default}">
<head>
    <meta charset="UTF-8">
    <title>帝一流</title>
    <link th:href="@{/static/plugin/bootstrap-table/bootstrap-table.css}" type="text/css" rel="stylesheet">
</head>
<body>
<div layout:fragment="cm-flex" class="cm-flex">
    <div class="cm-breadcrumb-container">
        <ol class="breadcrumb">
            <li><a href="#">库存管理</a></li>
            <li class="active">出库管理</li>
        </ol>
    </div>
    <form id="cm-search" action="index.html" method="get">
        <input type="search" name="q" autocomplete="off" placeholder="Search...">
    </form>
</div>
<div layout:fragment="content" class="container-fluid">
    <div class="panel panel-default">
        <div class="panel-body auto-height">
            <div class="search-block" id="search-block">
                <form action="#" class="form-group form-horizontal">
                    <div class="row">
                        <div class="col-sm-3 col-md-4">
                            <input type="text" class="form-control" placeholder="序列号/公司名称" id="search"
                                   data-toggle="completer" data-suggest="true">
                        </div>
                        <div class="col-sm-4 col-md-3">
                            <div class="input-group">
                                <input type="text" class="form-control" id="createTime" placeholder="出库时间">
                                <span class="input-group-btn">
                                        <button class="btn btn-default" type="button" id="btnTime">
                                             <img th:src="@{/static/img/sf/calendar-clock.svg}" height="18" width="18">
                                        </button>
                                </span>
                            </div>
                        </div>
                        <div class="col-sm-2 col-sm-offset-3 col-md-1 col-md-offset-4">
                            <button type="button" class="btn btn-primary btn-block" id="doSearch">查询</button>
                        </div>
                    </div>
                </form>
            </div>
            <div class="table-panel">
                <div class="sum">
                    <span class="sf-dashboard">&nbsp;</span><b id="sumWeight">: 0 kg</b>
                    <span class="sf-bitcoin">&nbsp;</span><b id="sumMoney">: 0 元</b>
                </div>
                <div class="btn-group" role="group" id="toolbar">
                    <button type="button" class="btn btn-default" id="addSold"><i class="fa fa-fw fa-plus"></i></button>
                    <button type="button" class="btn btn-default" id="editSold"><i class="fa fa-fw fa-pencil"></i></button>
                    <button type="button" class="btn btn-default" id="delSold"><i class="fa fa-fw fa-trash-o"></i></button>
                </div>
                <table id="soldTable"></table>
            </div>
        </div>
    </div>
</div>
<div layout:fragment="myJs">
    <div id="soldModal" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">×</span></button>
                    <h5 class="modal-title" id="soldModalTitle">
                        添加出库
                    </h5>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" id="soldForm">
                        <input type="hidden" id="id" name="id">
                        <div class="form-group">
                            <label for="company" class="col-sm-2 control-label">公司名称</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="company" name="company" placeholder="单位">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="serialNo" class="col-sm-2 control-label">序列号</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="serialNo" name="serialNo" placeholder="序列号">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="gross" class="col-sm-2 control-label">毛重</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="gross" name="gross" placeholder="毛重">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="tare" class="col-sm-2 control-label">去皮</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="tare" name="tare" placeholder="去皮">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="price" class="col-sm-2 control-label">单价</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="price" name="price" placeholder="单价">
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="saveSold">保存</button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>
    <script th:src="@{/static/plugin/bootstrap-table/bootstrap-table.js}"></script>
    <script th:inline="javascript">
        $(function () {
            var autoHeight = $("div.auto-height").height();
            var rowHeight = $('#search-block').height();
            var tableHeight = autoHeight - rowHeight;

            var $table = $('#soldTable');
            $table.bootstrapTable({
                // 条纹
                striped: true,
                height: tableHeight,
                toolbar: '#toolbar',
                pagination: true,
                paginationLoop: false,
                sidePagination: 'server',
                url: /*[[@{/grain/soldList}]]*/,
                method: 'post',
                contentType: "application/x-www-form-urlencoded",
                dataField: "data",
                queryParamsType: '',
                queryParams: function queryParams(params) {
                    var param = {
                        pageNo: params.pageNumber,
                        pageSize: params.pageSize,
                        createTime: $('#createTime').val(),
                        search: $('#search').val()
                    };
                    return param;
                },
                formatLoadingMessage: function () {
                    return "正在加载数据中，请稍候……"
                },
                formatShowingRows: function (a, b, c) {
                    return "第 " + a + " / " + b + " 条，共 " + c + " 条 ";
                },
                formatRecordsPerPage: function (a) {
                    return "每页 " + a;
                },
                formatNoMatches: function () {
                    return '没有找到匹配的数据';
                },
                onLoadSuccess:function(data){

                    $('#sumWeight').html(': ' + data['sumWeight'] + ' kg');
                    $('#sumMoney').html(': ' + data['sumMoney'] + ' 元');
                },
                columns: [
                    {
                        field: '',
                        checkbox: true
                    },
                    {
                        field: 'company',
                        title: '公司名称'
                    },
                    {
                        field: 'serialNo',
                        title: '序列号'
                    },
                    {
                        field: 'gross',
                        title: '毛重',
                        align: 'right'
                    },
                    {
                        field: 'tare',
                        title: '去皮',
                        align: 'right'
                    },
                    {
                        field: 'suttle',
                        title: '净重 (kg)',
                        align: 'center'
                    },
                    {
                        field: 'price',
                        title: '单价 (元/kg)',
                        align: 'center'
                    },
                    {
                        field: 'money',
                        title: '金额 (元)',
                        align: 'right',
                        formatter: function (value, row, index) {
                            value = value + '';
                            if (value.indexOf('.') > 0){
                                return value.split('.')[0];
                            }

                            return value;
                        }
                    },
                    {
                        field: 'createTime',
                        title: '创建时间',
                        align: 'center',
                        formatter: function (value, row, index) {
                            var str = '';
                            if (value) {
                                str = value.substr(0, 10);
                            }

                            return '<span style="cursor: default" title="' + value + '">' + str + '</span>';
                        }
                    }
                ]
            });

            laydate.render({
                elem: '#createTime'
                ,type: 'date'
                ,range: true
                ,eventElem: '#btnTime'
                ,trigger: 'click'
            });

            $('#doSearch').on('click', function () {
                $table.bootstrapTable("refresh");
            });

            $('#addSold').on('click', function () {
                $('#soldForm')[0].reset();

                $('#soldModalTitle').html('添加出库');
                $('#id').val('');
                $('#soldModal').modal('show');
            });

            $('#saveSold').on('click', function () {
                var param = $('#soldForm').serialize();
                $.ajax({
                    type: $('#id').val() == '' ? 'post' : 'put',
                    url: /*[[@{/grain/sold}]]*/,
                    data: param,
                    dataType: "json",
                    success: function (result) {
                        if (result == 1) {
                            $('#soldModal').modal('hide');
                            $('#soldTable').bootstrapTable('refresh');

                            alertTip('success', '保存成功');
                        } else {
                            alertTip('error', '保存失败');
                        }
                    }
                });
            });

            $('#editSold').on('click', function () {
                var solds = $table.bootstrapTable('getSelections');
                if (solds.length != 1) {
                    swal("请选择一条信息进行修改!");

                    return;
                }
                $('#soldForm')[0].reset();

                var sold = solds[0];
                $('#id').val(sold['id']);
                $('#company').val(sold['company']);
                $('#serialNo').val(sold['serialNo']);

                $('#tare').val(sold['tare']);
                $('#gross').val(sold['gross']);
                $('#price').val(sold['price']);

                $('#soldModalTitle').html('修改出库');
                $('#soldModal').modal('show');
            });

            $('#delSold').on('click', function () {
                var solds = $table.bootstrapTable('getSelections');
                if (solds.length < 1) {
                    swal("请至少选择一条信息进行删除!");

                    return;
                }
                // 提示
                swal({
                        title: "确定删除选中出库?",
                        text: "删除后将无法恢复",
                        type: "warning",
                        showCancelButton: true,
                        confirmButtonColor: "#e67e22",
                        confirmButtonText: "删除",
                        closeOnConfirm: true
                    },
                    function () {
                        var idArr = [];
                        for (var i = 0; i < solds.length; i++) {
                            idArr[i] = solds[i]['id'];
                        }
                        $.ajax({
                            type: 'delete',
                            url: /*[[@{/grain/sold}]]*/,
                            contentType: "application/json;charset=utf-8",
                            data: JSON.stringify(idArr),
                            dataType: "json",
                            success: function (result) {
                                if (result == 1) {
                                    $('#soldModal').modal('hide');
                                    $('#soldTable').bootstrapTable('refresh');

                                    alertTip('success', '删除成功');
                                } else {
                                    alertTip('error', '删除失败');
                                }
                            }
                        });
                    });
            });
        });
    </script>
</div>
</body>
</html>